package com.haiheng.library_base.view.shape;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;


import com.haiheng.library_base.R;

import androidx.appcompat.widget.AppCompatTextView;


/**
 * @author hwz
 * @time 2020-07-31
 * @describe 描边TextView
 */
public class ShapeTextView extends AppCompatTextView {
    private ShapeHelper shapeHelper;


    private int strokeWidth;

    private int strokeColor;
    private int solidColor;
    private int selectedTextColor;

    private int normalTextColor;
    private int defaultShapeColor = 0xffffffff;

    private boolean useFont;

    public ShapeTextView(Context context) {
        super(context);
        init(context,null,android.R.attr.textViewStyle);

    }

    public ShapeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
//        getAttr(context, attrs);
        init(context,attrs,android.R.attr.textViewStyle);
    }

    public ShapeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }



    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ShapeTextView, defStyleAttr, 0);
        // 公共
        float radius = typedArray.getDimensionPixelOffset(R.styleable.ShapeTextView_shapeCornersRadius, 0); // 圆角
        float radiusTopLeft = typedArray.getDimensionPixelOffset(R.styleable.ShapeTextView_shapeCornersTopLeftRadius, 0); // 左上圆角
        float radiusTopRight = typedArray.getDimensionPixelOffset(R.styleable.ShapeTextView_shapeCornersTopRightRadius, 0); // 右上圆角
        float radiusBottomRight = typedArray.getDimensionPixelOffset(R.styleable.ShapeTextView_shapeCornersBottomRightRadius, 0); // 右下圆角
        float radiusBottomLeft = typedArray.getDimensionPixelOffset(R.styleable.ShapeTextView_shapeCornersBottomLeftRadius, 0); // 左下圆角
        solidColor = typedArray.getColor(R.styleable.ShapeTextView_shapeSolidColor, 0); // 填充色
        strokeWidth = typedArray.getDimensionPixelOffset(R.styleable.ShapeTextView_shapeStrokeWidth, 0); // 描边宽度
        strokeColor = typedArray.getColor(R.styleable.ShapeTextView_shapeStrokeColor, 0); // 描边色
        // 按压
        int pressSolidColor = typedArray.getColor(R.styleable.ShapeTextView_shapeSelectorPressedColor, 0);// 填充色
        // 选中
        int selectedSolidColor = typedArray.getColor(R.styleable.ShapeTextView_shapeSelectorNormalColor, 0);// 填充色
        int selectedStrokeColor = typedArray.getColor(R.styleable.ShapeTextView_shapeSelectorNormalColor, 0);// 描边色

        //字体颜色
//        normalTextColor = getCurrentTextColor();
        selectedTextColor = typedArray.getColor(R.styleable.ShapeTextView_shapeSelectorNormalColor, 0);
        selectedTextColor = selectedTextColor == 0 ? normalTextColor : selectedTextColor;
        typedArray.recycle();

        shapeHelper = new ShapeHelper(this)
                .setRadius(new float[]{radiusTopLeft, radiusTopLeft, radiusTopRight, radiusTopRight, radiusBottomRight, radiusBottomRight, radiusBottomLeft, radiusBottomLeft})
                .setRadius(radius).setStrokeWidth(strokeWidth)
                .setNormalDrawable(strokeColor, solidColor)
                .setPressDrawable(0, pressSolidColor)
                .setSelectedDrawable(selectedStrokeColor, selectedSolidColor);
        shapeHelper.setNormalDrawable();
    }

  /*  private void getAttr(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.StrokeTextView);
        strokeWidth = typedArray.getDimensionPixelSize(R.styleable.StrokeTextView_stroketextview_stroke_width, 0);
        strokeColor = typedArray.getColor(R.styleable.StrokeTextView_stroketextview_stroke_color, defaultShapeColor);
        useFont = typedArray.getBoolean(R.styleable.StrokeTextView_stroketextview_use_font, false);

        // 公共
        float radius = typedArray.getDimensionPixelOffset(R.styleable.StrokeTextView_shapeCornersRadius, 0); // 圆角
        float radiusTopLeft = typedArray.getDimensionPixelOffset(R.styleable.StrokeTextView_shadowCornersTopLeftRadius, 0); // 左上圆角
        float radiusTopRight = typedArray.getDimensionPixelOffset(R.styleable.StrokeTextView_shadowCornersTopRightRadius, 0); // 右上圆角
        float radiusBottomRight = typedArray.getDimensionPixelOffset(R.styleable.StrokeTextView_shadowCornersBottomRightRadius, 0); // 右下圆角
        float radiusBottomLeft = typedArray.getDimensionPixelOffset(R.styleable.StrokeTextView_shadowCornersBottomLeftRadius, 0); // 左下圆角
        solidColor = typedArray.getColor(R.styleable.StrokeTextView_shapeSolidColor, 0); // 填充色
        // 按压
        int pressSolidColor = typedArray.getColor(R.styleable.StrokeTextView_shapeSelectorPressedColor, 0);// 填充色
        // 选中
        int selectedSolidColor = typedArray.getColor(R.styleable.StrokeTextView_shapeSelectorNormalColor, 0);// 填充色
        int selectedStrokeColor = typedArray.getColor(R.styleable.StrokeTextView_shapeSelectorNormalColor, 0);// 描边色
        typedArray.recycle();

        shapeHelper = new ShapeHelper(this)
                .setRadius(new float[]{radiusTopLeft, radiusTopLeft, radiusTopRight, radiusTopRight, radiusBottomRight, radiusBottomRight, radiusBottomLeft, radiusBottomLeft})
                .setRadius(radius).setStrokeWidth(strokeWidth)
                .setNormalDrawable(strokeColor, solidColor)
                .setPressDrawable(0, pressSolidColor)
                .setSelectedDrawable(selectedStrokeColor, selectedSolidColor);
        shapeHelper.setNormalDrawable();

    }

*/


    @Override
    public void setPressed(boolean pressed) {
        super.setPressed(pressed);
        if (pressed) {
            shapeHelper.setPressDrawable();
        }
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            shapeHelper.setSelectedDrawable();
//            setTextColor(selectedTextColor);
        } else {
            shapeHelper.setNormalDrawable();
//            setTextColor(normalTextColor);
        }
    }

    public void setSolidColor(int color) {
        this.solidColor = color;
        shapeHelper.setNormalDrawable(strokeColor, color);
    }

    public void setStrokeColor(int strokeColor) {
        this.strokeColor = strokeColor;
        shapeHelper.setStrokeColor(strokeColor);
        shapeHelper.setNormalDrawable(strokeColor, solidColor);
    }
    public void setStrokeWidth(int strokeWidth) {
        shapeHelper.setStrokeWidth(strokeWidth);
    }

    public void setRadius(int radius) {
        shapeHelper.setRadius(radius);
    }


}